package com.link2room.aeriabots.facade.impl;

import com.link2room.aeriabots.dto.param.inside.WxFansInsideParamDto;
import com.link2room.aeriabots.dto.param.inside.WxQrcodeInsideParamDto;
import com.link2room.aeriabots.dto.result.base.WxFansInsideResDto;
import com.link2room.aeriabots.facade.IWxFansFacadeService;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.StringUtil;
import com.link2room.aeriabots.entity.WxFans;
import com.tencent.protocol.officialAccount.FansGet.FansGetResDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LN on 2016/10/17.
 */
@Service("wxFansFacadeService")
public class WxFansFacadeServiceImpl extends BaseFacade implements IWxFansFacadeService {
    @Resource
    protected ICommonService commonService;
    @Override
    public WxFansInsideResDto getWxFans(WxFansInsideParamDto paramDto) throws Exception {
        String openid = paramDto.getOpenid();
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        if( StringUtil.isBlank(openid)||StringUtil.isBlank(lhotelGroupCode)){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+" [openid]:"+openid+"\n[lhotelGroupCpde]:"+"\n"+lhotelGroupCode, BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        WxFans wfc = new WxFans();
        wfc.setOpenid(openid);
        wfc.setLhotelGroupCode(lhotelGroupCode);
        WxFans res = commonService.findOneEQ(WxFans.class, wfc);
        if(res==null){
            WxFansInsideResDto resDto = new WxFansInsideResDto();
            resDto.setOpenid(openid);
            resDto.setLhotelGroupCode(lhotelGroupCode);
            return resDto;
        }else{
            String sceneStr = res.getSceneStr();
            String salesman = "";
            if(StringUtil.isNotBlank(sceneStr)){
                salesman =  WxQrcodeInsideParamDto.splitToGetKey(sceneStr);
            }
            WxFansInsideResDto resDto = new WxFansInsideResDto();
            resDto.setOpenid(openid);
            resDto.setSalesman(salesman);
            resDto.setLhotelGroupCode(res.getLhotelGroupCode());
            return resDto;
        }
    }

    @Override
    public void wxFansInsert(FansGetResDate data) throws Exception {
        FansGetResDate._OPENID datas = data.getData();
        if(datas != null){
            List<String> openids = datas.getOpenid();
            List<WxFans> wxFansList = new ArrayList<WxFans>();
            if(openids!= null && openids.size() > 0){
                for (String opneid:openids) {
                    WxFans wx = new WxFans();
                    wx.setLhotelGroupCode(DefaultValue.LHOTEL_GROUP_CODE);
                    wx.setLhotelCode(DefaultValue.LHOTEL_CODE);
                    wx.setOpenid(opneid);
                    wxFansList.add(wx);
                }
            }
            if(wxFansList.size()>0){
                commonService.saveList(wxFansList);
            }
        }
    }
}
